import Heibaipei from "../heibaipei/Heibaipei";
import Enemy from "./Enemy";

const {ccclass, property} = cc._decorator;

// 所有子弹的基类，所有子弹都继承该类
@ccclass
export default class Bullet extends cc.Component {

    @property()
    speed: number = 5; // 速度
    attack: number = 1;

    start() {

    }

    update(dt) {
        this.move();
        this.myDestroy();

    }



    onCollisionEnter(other, self) {
        console.log("子弹碰撞")
        if (self.node.group == "bullet" && other.node.group == "enemy") {
            this.node.destroy();
            let enemy = other.node.getComponent("Enemy");
            enemy.beInjured(this.attack);
        }
        if (self.node.group == "enemyBullet" && other.node.group == "player") {
            this.node.destroy();
            let player = other.node.getComponent("Player");
            player.beInjured(this.attack);
        }
    }

    protected move(){
        this.node.y += this.speed;
    }

    protected myDestroy(){
        if (this.node.y > 1000) {
            this.node.destroy();
        }
    }

    protected onDestroy(): void {
        console.log("子弹被销毁");
    }
}
